﻿using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;

namespace Website.Models.HtmlHelpers
{
    public static class Helpers
    {
        public static MvcHtmlString GetCategories(this HtmlHelper html, IEnumerable<Category> categories,
                                                  int currentCategoryID)
        {
            TagBuilder selectTag = new TagBuilder("select");
            selectTag.MergeAttribute("name", "category");
            selectTag.MergeAttribute("id", "category");
            TagBuilder optionTextTag = new TagBuilder("option");
            optionTextTag.MergeAttribute("value","-1");
            optionTextTag.InnerHtml = "All";
            selectTag.InnerHtml += optionTextTag.ToString();
            foreach (Category c in categories)
            {
                TagBuilder optionTag = new TagBuilder("option");
                optionTag.MergeAttribute("value", c.CategoryID.ToString());
                optionTag.InnerHtml = c.CategoryName;
                if (currentCategoryID == c.CategoryID)
                    optionTag.MergeAttribute("selected", "selected");
                selectTag.InnerHtml += optionTag.ToString();
                if (c.SubCategories.Count <= 0) continue;
                foreach (Category sub in c.SubCategories)
                {
                    TagBuilder optionSubTag = new TagBuilder("option");
                    optionSubTag.MergeAttribute("value", sub.CategoryID.ToString());
                    optionSubTag.InnerHtml = "+ " + sub.CategoryName;
                    if (currentCategoryID == sub.CategoryID)
                        optionSubTag.MergeAttribute("selected", "selected");
                    selectTag.InnerHtml += optionSubTag.ToString();
                }
            }
            return MvcHtmlString.Create(selectTag.ToString());
        }
    }
}